<template>
  <ContentWrap v-hasRole="['shop_admin']">
    <div class="flex flex-row flex-wrap">
      <el-button plain type="primary" @click="openForm('create')">
        <Icon class="mr-5px" icon="ep:plus" />
        新增配置
      </el-button>
      <div class="flex-1"></div>
      <el-button @click="resetQuery">
        <Icon class="mr-5px" icon="ep:refresh" />
        刷新列表
      </el-button>
    </div>
  </ContentWrap>
  <ContentWrap>
    <div class="mt-[5px]">
      <el-table :data="tableData.list" size="large" v-loading="tableData.loading">
        <template #empty>
          <span>{{ !tableData.loading ? '没有相关数据' : '' }}</span>
        </template>
        <el-table-column
          v-hasRole="['super_admin', 'platform_admin', 'tenant_admin']"
          label="所属店铺"
          min-width="100"
        >
          <template #default="{ row }">
            <div>{{ row.shop?.name || row.shopId }}</div>
          </template>
        </el-table-column>
        <el-table-column label="引流赠品" min-width="100">
          <template #default="{ row }">
            <div>
              {{
                row.giftActivityNames && row.giftActivityNames.length > 0
                  ? row.giftActivityNames.join('、')
                  : '未知'
              }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="满送条件" min-width="100">
          <template #default="{ row }">
            <div
              >下单满<span class="text-orange-500">{{ fenToYuan(row.giveUsePrice) }}元</span>后赠送
            </div>
          </template>
        </el-table-column>
        <el-table-column
          label="创建时间"
          align="center"
          prop="createTime"
          :formatter="dateFormatter"
          min-width="170"
        />
        <el-table-column
          v-hasRole="['shop_admin']"
          label="操作"
          min-width="100"
          align="center"
          fixed="right"
        >
          <template #default="scope">
            <el-button link type="primary" @click="openForm('update', scope.row.id)">
              编辑
            </el-button>
            <el-button link type="danger" @click="handleDelete(scope.row.id)"> 删除 </el-button>
          </template>
        </el-table-column>
      </el-table>
      <Pagination
        v-model:limit="tableData.searchParam.pageSize"
        v-model:page="tableData.searchParam.pageNo"
        :total="tableData.total"
        @pagination="loadTableData"
      />
    </div>
  </ContentWrap>

  <GiftScanConfigForm ref="formRef" @success="loadTableData" />
</template>

<!--suppress HtmlUnknownAttribute -->
<script lang="ts" setup name="GiftScanConfigList">
import * as GiftScanConfigApi from '@/api/promotion/gift/GiftScanConfigApi'
import { FormInstance } from 'element-plus'
import { dateFormatter } from '@/utils/formatTime'
import GiftScanConfigForm from './GiftScanConfigForm.vue'
import { fenToYuan } from '@/utils'

const { t } = useI18n()
const message = useMessage()

const tableData = reactive({
  total: 0,
  loading: true,
  list: [],
  searchParam: {
    pageNo: 1,
    pageSize: 10,
    createTime: []
  }
})

const loadTableData = () => {
  tableData.loading = true
  GiftScanConfigApi.getPage({
    ...tableData.searchParam
  })
    .then((res: any) => {
      tableData.loading = false
      tableData.list = res.list
      tableData.total = res.total
    })
    .catch(() => {
      tableData.loading = false
    })
}

const searchFormRef = ref<FormInstance>()

const handleQuery = () => {
  tableData.searchParam.pageNo = 1
  loadTableData()
}

const resetQuery = () => {
  searchFormRef.value?.resetFields()
  handleQuery()
}

const formRef = ref()
const openForm = (type: string, id?: number | string) => {
  formRef.value.open(type, id)
}

const handleDelete = async (id: number | string) => {
  try {
    await message.delConfirm()
    await GiftScanConfigApi.deleteById(id)
    message.success(t('common.delSuccess'))
    loadTableData()
  } catch {}
}

onMounted(() => {
  handleQuery()
})
</script>
